package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.baidu.navisdk.adapter.BNOuterLogUtil;
import com.baidu.navisdk.adapter.BNOuterTTSPlayerCallback;
import com.baidu.navisdk.adapter.BNRoutePlanNode;
import com.baidu.navisdk.adapter.BaiduNaviManager;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.SharedPreferencesUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/7.
 */

public class Map extends AppCompatActivity {
    Button mapbtn;
    SharedPreferencesUtil sharedPreferencesUtil;
    Activity mActivity;
    private Button mGcjNaviBtn = null;
    private String mSDCardPath = null;
    private static final String APP_FOLDER_NAME = "BNSDKDemo";
    public static final String ROUTE_PLAN_NODE = "routePlanNode";
    String authinfo = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maplayout);
        mActivity = this;
        init();

        BNOuterLogUtil.setLogSwitcher(true);
        if (initDirs()) {
            Log.e("ssss", "initNavi");
            initNavi();
        }

    }

    private void init() {
        mapbtn = (Button) this.findViewById(R.id.mapbtn);
        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(this);

        mapbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("ssss", "sdsdsd");
                routeplanToNavi(118.161024, 24.509077, BNRoutePlanNode.CoordinateType.GCJ02);

            }
        });
    }

    private void initNavi() {

        BNOuterTTSPlayerCallback ttsCallback = null;

        BaiduNaviManager.getInstance().init(mActivity, mSDCardPath, APP_FOLDER_NAME,
                new BaiduNaviManager.NaviInitListener() {
                    @Override
                    public void onAuthResult(int status, final String msg) {
                        if (0 == status) {
                            authinfo = "key校验成功!";
                            Log.e("ssss", "key校验成功");
                        } else {
                            authinfo = "key校验失败, " + msg;
                            Log.e("ssss", "key校验失败");
                        }
                        mActivity.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
//                                Toast.makeText(mActivity, authinfo, Toast.LENGTH_LONG).show();
                                Log.e("ssss", "Map_authinfo=="+authinfo);
                            }
                        });

                    }

                    public void initSuccess() {
//                        Toast.makeText(mActivity, "百度导航引擎初始化成功", Toast.LENGTH_SHORT).show();
                        Log.e("ssss", "Map_百度导航引擎初始化成功");
                    }

                    public void initStart() {
//                        Toast.makeText(mActivity, "百度导航引擎初始化开始", Toast.LENGTH_SHORT).show();
                        Log.e("ssss", "Map_百度导航引擎初始化开始");
                    }

                    public void initFailed() {
//                        Toast.makeText(mActivity, "百度导航引擎初始化失败", Toast.LENGTH_SHORT).show();
                        Log.e("ssss", "Map_百度导航引擎初始化失败");
                    }
                }, null /*mTTSCallback*/);
    }

    private boolean initDirs() {
        mSDCardPath = getSdcardDir();
        if (mSDCardPath == null) {
            return false;
        }
        File f = new File(mSDCardPath, APP_FOLDER_NAME);
        if (!f.exists()) {
            try {
                f.mkdir();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    private String getSdcardDir() {
        if (Environment.getExternalStorageState().equalsIgnoreCase(
                Environment.MEDIA_MOUNTED)) {
            return Environment.getExternalStorageDirectory().toString();
        }
        return null;
    }

    public void routeplanToNavi(double jingdu, double weidu, BNRoutePlanNode.CoordinateType coType) {
        BNRoutePlanNode sNode = null;
        BNRoutePlanNode eNode = null;
        Log.e("url", "目的地址:经度==" + jingdu + " 纬度==" + weidu);

        switch (coType) {
            case GCJ02: {
                Log.e("ssss", "导航");
                sNode = new BNRoutePlanNode(Double.parseDouble(sharedPreferencesUtil.getJingdu()), Double.parseDouble(sharedPreferencesUtil.getWeidu()), "目前地址", null, coType);

                eNode = new BNRoutePlanNode(jingdu, weidu, "目的地", null, coType);
                Log.e("url", "sNode==" + sNode + "   eNode==" + eNode);
                Log.e("url", "当前地址:纬度==" + sharedPreferencesUtil.getJingdu() + " 经度==" + sharedPreferencesUtil.getWeidu());
                Log.e("url", "目的地址:经度==" + jingdu + " 纬度==" + weidu);
                break;
            }
//            case WGS84: {
//                sNode = new BNRoutePlanNode(116.300821, 40.050969,
//                        "百度大厦", null, coType);
//                eNode = new BNRoutePlanNode(116.397491, 39.908749,
//                        "北京天安门", null, coType);
//                break;
//            }
//            case BD09_MC: {
//                sNode = new BNRoutePlanNode(12947471, 4846474,
//                        "百度大厦", null, coType);
//                eNode = new BNRoutePlanNode(12958160, 4825947,
//                        "北京天安门", null, coType);
//                break;
//            }
            default:

        }
        if (sNode != null && eNode != null) {
            List<BNRoutePlanNode> list = new ArrayList<BNRoutePlanNode>();
            list.add(sNode);
            list.add(eNode);
            BaiduNaviManager.getInstance().launchNavigator(mActivity, list, 1, true, new DemoRoutePlanListener(sNode));
        }
    }

    public class DemoRoutePlanListener implements BaiduNaviManager.RoutePlanListener {

        private BNRoutePlanNode mBNRoutePlanNode = null;

        public DemoRoutePlanListener(BNRoutePlanNode node) {
            Log.e("url", "DemoRoutePlanListener");
            mBNRoutePlanNode = node;
            Log.e("url", "mBNRoutePlanNode==" + mBNRoutePlanNode);
        }

        @Override
        public void onJumpToNavigator() {
            Log.e("url", "onJumpToNavigator_BNDemoGuideActivity");
            Intent intent = new Intent(mActivity, BNDemoGuideActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable(ROUTE_PLAN_NODE, (BNRoutePlanNode) mBNRoutePlanNode);
            intent.putExtras(bundle);
            startActivity(intent);
        }

        @Override
        public void onRoutePlanFailed() {
            // TODO Auto-generated method stub
            Log.e("url", "onRoutePlanFailed");
        }

    }
}
